package com.ruoyi.crop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Crop;
import com.ruoyi.crop.api.domain.CropType;
import com.ruoyi.crop.api.domain.Farmer;
import com.ruoyi.crop.service.CropService;
import com.ruoyi.crop.service.CropTypeService;
import com.ruoyi.crop.mapper.CropTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 86178
* @description 针对表【src_crop_type(农作物类别表)】的数据库操作Service实现
* @createDate 2024-07-27 09:35:55
*/
@Service
public class CropTypeServiceImpl extends ServiceImpl<CropTypeMapper, CropType>
    implements CropTypeService{
    @Autowired
    CropService cropService;

    /**
     * 获取农作物类别列表
     */
    @Override
    public List<CropType> selectCropTypeList(CropType cropType) {
        LambdaQueryWrapper<CropType> queryWrapper = new LambdaQueryWrapper<>();
        if (cropType!=null){
            //品种
            queryWrapper.eq(StringUtils.isNotBlank(cropType.getCropTypeVariety()),CropType::getCropTypeVariety,cropType.getCropTypeVariety());
            //健康状况
            queryWrapper.eq(StringUtils.isNotBlank(cropType.getHealthStatus()),CropType::getHealthStatus,cropType.getHealthStatus());
            queryWrapper.orderByAsc(CropType::getCropTypeId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 新增农作物类别
     */
    @Override
    public boolean saveCropType(CropType cropType) {
        LambdaQueryWrapper<Crop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Crop::getName,cropType.getCropName());
        Crop crop = cropService.getOne(queryWrapper);
        //农作物名称
        cropType.setCropCode(crop.getCropCode());
        cropType.setCreateBy(SecurityUtils.getUsername());
        cropType.setCreateTime(new Date());
        return this.save(cropType);
    }

    /**
     * 修改农作物类别
     */
    @Override
    public boolean updateCropTypeById(CropType cropType) {
        LambdaQueryWrapper<Crop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Crop::getCropCode,cropType.getCropCode());
        Crop crop = cropService.getOne(queryWrapper);
        //农作物名称
        cropType.setCropName(crop.getName());
        cropType.setUpdateBy(SecurityUtils.getUsername());
        cropType.setUpdateTime(new Date());
        return this.updateById(cropType);
    }
}




